
var jude = Require(path.join(__dirname, 'verifty.js'));
// var optionSets = require('/component/optionSets');

var selectOptions = function (key, data, parent) {
    this.parent = parent;
    this.data = data;
    this.name = data[key];
    this.html = $('<div class="form_inputSelect_option">' + this.name + '</div>');
    this.selectStutas = false;
    var _this = this;
    this.html.unbind().bind('click', function (e) {
        e.stopPropagation();
        $(document).click();
        _this.change();
    });
};
selectOptions.prototype = {
    constructor: this,
    render: function () {
        return this.html
    },
    select: function () {
        this.selectStutas = true;
        this.html.addClass('active');
        this.parent.value.val(this.data[this.parent.options.showNameByDataKey]);
        this.parent.set(this.data, 'object', false, false, true);
        this.html.unbind();
    },
    removeSelect: function () {
        var _this = this;
        this.selectStutas = false;
        this.html.removeClass('active');
        this.html.unbind().bind('click', function (e) {
            e.stopPropagation();
            $(document).click();
            _this.change();
        });
    },
    change: function () {
        if (this.parent.layout) {
            this.parent.layout.hideError();
        };
        for (var i = 0; i < this.parent.child.length; i++) {
            var element = this.parent.child[i];
            element.removeSelect();
        };
        this.select();
        this.parent.options.changeEvent(this.parent);
    }
};
var inputSelect = function (options, layout) {
    this.options = {
        showNameByDataKey: 'label',
        setValueByDataKey: 'value',
        data: [],
        'default': null,
        readonly: false,
        jude: {
            name: '区域',
            rules: ['notNull', 'isNumber']
        },
        getForm: 'id',
        formatNewString: 'new_',
        placeholder: '- 请选择 -',
        changeEvent: function (e) { },
    };
    this.options = $.extend({}, this.options, options);

    this.valueData = null;
    var _this = this;
    var html = '';
    html += '   <div class="form_inputSelect ' + (this.options.size ? this.options.size : '') + '">';
    html += '       <div class="form_inputSelect_valueBox">';
    html += '           <input type="text" class="form_inputSelect_value"/>'; //选中项
    html += '           <span></span>';
    html += '       </div>';
    html += '       <div class="form_inputSelect_optionBox"></div>';
    html += '   </div>';
    this.html = $(html);
    this.nameBox = this.html.find('.form_inputSelect_valueBox');
    this.dateBox = this.html.find('.form_inputSelect_optionBox');
    this.value = this.html.find('.form_inputSelect_value');

    var showOptionBox = false;
    this.cache = null;
    var documentClick = function () {
        _this.html.removeClass('active');
        showOptionBox = false;
        _this.dateBox.css('display', 'none');
        $(document).unbind('click', documentClick);
    };
    this.nameBox.click(function (e) {
        e.stopPropagation();
        if (showOptionBox) {
            documentClick();
        } else {
            _this.html.addClass('active');
            showOptionBox = true;
            _this.dateBox.css('display', 'block');
            _this.value.focus();
            var value = _this.value.val();
            if (value == _this.options.placeholder) {
                _this.value.val('')
            };
            $(document).click();
            $(document).bind('click', documentClick);
        };
    });
    this.layout = layout;
    this.defaultValue();
    this.setData(this.options.data);


    if (this.layout) {
        this.layout.append(this.html);
        if (this.options.className) {
            this.layout.html.addClass(this.options.className);
        };
    } else {
        if (this.options.className) {
            this.html.addClass(this.options.className);
        };
    };
    this.value.unbind().bind('input propertychange', function () {
        var value = $(this).val();
        if (!jude.notNull.perform(value)) {
            value = null;
        };
        _this.set(value, 'string', true);
    });
    this.value.click(function (e) {
        e.stopPropagation();
        if (!_this.html.hasClass('active')) {
            _this.nameBox.click();
        };
    });

    this.value.blur(function () {
        var value = $(this).val();
        if (!jude.notNull.perform(value)) {
            value = null;
        };
        _this.set(value, 'string', false, true);
    });
};
inputSelect.prototype = {
    constructor: this,
    render: function () {
        return this.html;
    },
    get: function (callBack) {
        debugger;
        var _this = this;
        if (this.valueData !== null && jude.type(this.valueData, 'object')) {
            callBack(false, this.valueData[this.options.getForm]);
        } else {
            var start = this.checkRules(this.valueData);
            callBack(start, this.options.formatNewString + this.valueData);
        };
    },
    set: function (data, type, isChange, isBlur, isSelect) {
        if (isSelect) {
            this.valueData = data;
            return;
        };
        if (data === null) {
            if (isBlur) {
                this.valueData = null;
                this.value.val(this.options.placeholder);
            };
            return;
        };
        for (var i = 0; i < this.child.length; i++) {
            var element = this.child[i];
            element.removeSelect();
        };
        var isComplete = false;
        if (type == 'object') {
            for (var i = 0; i < this.child.length; i++) {
                var element = this.child[i];
                if (jude.isEqual(element.data, data)) {
                    element.select();
                    isComplete = true;
                    this.valueData = data;
                    break;
                };
            };
        } else if (type == 'length') {
            if (this.child.length > data) {
                var element = this.child[data];
                element.select();
                this.valueData = element.data;
                isComplete = true;
            };
        } else if (type == 'setValueByDataKey') {
            for (var i = 0; i < this.child.length; i++) {
                var element = this.child[i];
                var _data = element.data[this.options.setValueByDataKey]
                if (_data == data) {
                    element.select();
                    isComplete = true;
                    this.valueData = element.data;
                    break;
                };
            };
        } else if (type == 'string' || type == undefined) {
            for (var i = 0; i < this.child.length; i++) {
                var element = this.child[i];
                var _data = element.data[this.options.showNameByDataKey]
                if (_data == data) {
                    element.select();
                    isComplete = true;
                    this.valueData = element.data;
                    break;
                };
            };
        };
        if (isComplete) {
            this.value.val(this.valueData[this.options.showNameByDataKey]);
        } else {
            this.valueData = data;
            this.value.val(data);
        };
    },
    checkRules: function (value) {
        var status = null;
        var _this = this;
        if ($.inArray("notNull", _this.options.jude.rules) == -1 && value == '') {
            return false;
        };
        for (var i = 0; i < this.options.jude.rules.length; i++) {
            var item = this.options.jude.rules[i];
            if (!jude[item].perform(value)) {
                status = (this.options.jude.name || '') + jude[item].text;
                break;
            };
        };
        return status;
    },
    setData: function (data) {
        this.options.data = data;
        var _this = this;
        if (jude.type(this.options.data, 'string')) {
            console.error('暂时作废');
            // var optionsName = this.options.data.split(':');
            // if (optionsName[1]) {
            //     optionSets(optionsName[1], function(result) {
            //         _this.setData(result);
            //         _this.defaultValue();
            //         if (_this.cache !== null) {
            //             _this.set(_this.cache);
            //         };
            //     }, 'list');
            // };
        } else if (jude.type(this.options.data, 'array')) {
            // this.cache = data;
            this.data = data;
            this.child = [];
            this.dateBox.empty();
            this.valueData = null;
            this.options.data = data;
            for (var i = 0; i < data.length; i++) {
                var item = data[i];
                var child = new selectOptions(this.options.showNameByDataKey, item, this);
                this.child.push(child);
                this.dateBox.append(child.render());
            };
            this.defaultValue();
            if (jude.type(this.relevance, 'function')) {
                this.relevance();
            };

        };
    },
    defaultValue: function () {
        if (this.options['default'] === null) {
            this.value.val(this.options.placeholder);
        } else {
            this.set(this.options['default']);
        };
    },
};
module.exports = function (options, layout) {
    return new inputSelect(options, layout);
}
